<?php
namespace addon\wechat_config\logic;
// +----------------------------------------------------------------------
// | Author: xiaojun <920668625@qq.com>
// +----------------------------------------------------------------------
use app\common\logic\LogicBase;
use think\Db;
use \tpfcore\Core;
use addon\wechat_material\logic\WechatMaterial;

class WechatReceive extends LogicBase
{


    /*响应图片消息*/
    public function receiveImageFunc($object){

        $content = ['MediaId'=>$object->MediaId];
        $result  = WechatFunc::transmitImage($object, $content);

        return $result;
    }

    /*响应链接消息*/
    public function receiveLinkFunc($object){

        $content = '你发送的是链接，标题为：'.$object->Title.'；内容为：'.$object->Description.'；链接地址为：'.$object->Url;
        $result = WechatFunc::transmitText($object, $content);

        return $result;
    }

    /*响应位置消息*/
    public function receiveLocationFunc($object){

        $content = '你发送的是位置，经度为：'.$object->Location_Y.'；纬度为：'.$object->Location_X.'；缩放级别为：'.$object->Scale.'；位置为：'.$object->Label;
        $result = WechatFunc::transmitText($object, $content);

        return $result;
    }

    /*响应视频消息*/
    public function receiveVideoFunc($object){

        $content = [
            'MediaId'=>$object->MediaId,
            'ThumbMediaId'=>$object->ThumbMediaId,
            'Title'=>'',
            'Description'=>''
        ];
        $result = WechatFunc::transmitVideo($object, $content);

        return $result;
    }

    /*响应语音消息*/
    public function receiveVoiceFunc($object){

        if (isset($object->Recognition) && !empty($object->Recognition))
        {
            //$content = '你刚才说的是：'.$object->Recognition;
            //图灵机器人
            $content = WechatRobot::tuling($object->Recognition);
            $result  = WechatFunc::transmitText($object, $content);
        }else{
            
            $content = ['MediaId'=>$object->MediaId];
            $result  = WechatFunc::transmitVoice($object, $content);
        }

        return $result;
    }


    /*=============================完美分隔线==================================*/

    static public function materialKeywordList($keyword='')
    {
        $keyword = htmlspecialchars($keyword);
        
        $data = array();
        $AutoReplyDb = Db::name('WechatAutoReply');
        $info = $AutoReplyDb->where(['keyword_type'=>1,'keyword'=>$keyword])->order('id desc')->find();
        if(!$info){
            $info = $AutoReplyDb->where(['keyword_type'=>0,'keyword'=>['like','%'.$keyword.'%']])->order('id desc')->find();
        }
        if(empty($info)){
            return $data;
        }
        defined('WECHAT_REPLY_TYPE') or define('WECHAT_REPLY_TYPE',$info['material_type']);
        switch($info['material_type']){

            case 'text':
                $data = strip_tags($info['content'],'</a>');
                break;
            case 'image':
                if($info){
                    $data = (new WechatMaterial())->getImageMaterialByMediaId($info['media_id']);
                }
                break;
            case 'voice':
                if($info){
                    $data = (new WechatMaterial())->getVoiceMaterialByMediaId($info['media_id']);
                }
                break;
            case 'video':
                if($info){
                    $data = (new WechatMaterial())->getVideoMaterialByMediaId($info['media_id']);
                }
                break;
            default:
                if($info){
                    $data = (new WechatMaterial())->getNewsMaterialByMediaId($info['media_id']);
                }
            break;
        }

        return $data;
    }

    /**
     * 处理回复内容转XML
     * @param  [type] $content [description]
     * @return [type]          [description]
     */
    static public function DisposeContent($object,$content){

        if(is_array($content))
        {
            if(isset($content[0]) && is_array($content[0]))
            {
                switch($content[0]['MsgType'])
                {
                    case 'image':
                        $result = WechatFunc::transmitImage($object, $content);
                        break;
                    case 'voice':
                        $result = WechatFunc::transmitMusic($object, $content);
                        break;
                    case 'video':
                        $result = WechatFunc::transmitVideo($object, $content);
                        break;
                    case 'news':
                        $result = WechatFunc::transmitNews($object, $content);
                        break;
                }
            }else{
                switch($content['MsgType'])
                {
                    case 'image':
                        $result = WechatFunc::transmitImage($object, $content);
                        break;
                    case 'voice':
                        $result = WechatFunc::transmitMusic($object, $content);
                        break;
                    case 'video':
                        $result = WechatFunc::transmitVideo($object, $content);
                        break;
                    case 'news':
                        $result = WechatFunc::transmitNews($object, $content);
                        break;
                }
            }
        }else{
            $result = WechatFunc::transmitText($object, $content);
        }

        return $result;
    }
}